package com.yj.ccs.abs.web.asset.mapper;

import com.yj.ccs.abs.web.asset.entity.Asset;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

/**
 * Copyright 2018 (C) Yunjian-VC
 * Created on : 2018/3/20
 * Author     : Unbelievable9
 **/
@Repository
@Mapper
public interface AssetMapper {

    @Options(useGeneratedKeys = true, keyProperty = "item.id")
    @Insert("INSERT INTO `asset` (number, money_sum, start_date, predict_date, `level`, " +
            "insured, insurance_coverage, guarantee, deposited, money_deposited, expanded, " +
            "money_break, accused, `status`, create_time)" +
            "VALUES (#{item.number}, #{item.money_sum}, #{item.start_date}, " +
            "#{item.predict_date}, #{item.level}, #{item.insured}, #{item.insurance_coverage}, " +
            "#{item.guarantee}, #{item.deposited}, #{item.money_deposited}, #{item.expanded}, " +
            "#{item.money_break}, #{item.accused}, #{item.status}, NOW())")
    void create(@Param("item") Asset item);

    @Update("UPDATE `asset` SET `level` = #{item.level}, insured = #{item.insured}, " +
            "insurance_coverage = #{item.insurance_coverage}, guarantee = #{item.guarantee}, " +
            "deposited = #{item.deposited}, money_deposited = #{item.money_deposited}, expanded = #{item.expanded} " +
            "WHERE id = #{item.id}")
    void update(@Param("item") Asset item);

    @Update("UPDATE `asset` SET is_settled = #{is_settled} WHERE id = #{id}")
    void updateIsSettledById(@Param("is_settled") Boolean is_settled, @Param("id") Long id);

    @Update("UPDATE `asset` SET available_money = #{available_money} WHERE id = #{id}")
    void updateAvailableMoneyById(@Param("available_money") BigDecimal available_money, @Param("id") Long id);

    @Update("<script>" +
            "UPDATE asset SET `status` = #{status} " +
            "WHERE id IN (" +
            "<foreach collection = 'list' item = 'id' separator = ', '>" +
            "#{id}" +
            "</foreach>" +
            ")" +
            "</script>")
    void batchUpdateStatus(@Param("list") List<Long> list, @Param("status") Integer status);

    @Update("<script>" +
            "UPDATE asset SET pool_date = #{pool_date} " +
            "WHERE id IN (" +
            "<foreach collection = 'list' item = 'id' separator = ', '>" +
            "#{id}" +
            "</foreach>" +
            ")" +
            "</script>")
    void batchUpdatePoolDate(@Param("pool_date") LocalDate pool_date, @Param("list") List<Long> list);

    @Update("<script>" +
            "UPDATE asset SET recycle_date = #{recycle_date} " +
            "WHERE id IN (" +
            "<foreach collection = 'list' item = 'id' separator = ', '>" +
            "#{id}" +
            "</foreach>" +
            ")" +
            "</script>")
    void batchUpdateRecyclelDate(@Param("recycle_date") LocalDate recycle_date, @Param("list") List<Long> list);

    @Select("SELECT * FROM `asset` WHERE id = #{id} LIMIT 1")
    Asset getById(@Param("id") Long id);

    @Select("<script>" +
            "SELECT * FROM `asset` WHERE id IN (" +
            "<foreach collection = 'ids' item = 'id' separator = ', '>" +
            "#{id}" +
            "</foreach>" +
            ")" +
            "</script>")
    List<Asset> getByIds(@Param("ids") List<Long> ids);
}
